package dao;

import java.util.List;

import idao.IProductsDAO;
import model.Products;

import org.hibernate.Query;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

@Repository
public class ProductsDAO implements IProductsDAO {

	@Autowired
	private SessionFactory sessionFactory;

	public SessionFactory getSessionFactory() {
		return sessionFactory;
	}

	public void setSessionFactory(SessionFactory sessionFactory) {
		this.sessionFactory = sessionFactory;
	}

	public ProductsDAO() {
		// TODO Auto-generated constructor stub
	}

	public boolean insert(Products products) {
		// TODO Auto-generated method stub
		try {
			sessionFactory.getCurrentSession().save(products);
			return true;
		} catch (Exception e) {
			// TODO: handle exception
		}
		return false;
	}

	public boolean update(Products products) {
		// TODO Auto-generated method stub
		try {
			sessionFactory.getCurrentSession().update(products);
			return true;
		} catch (Exception e) {
			// TODO: handle exception
		}
		return false;
	}

	public boolean delete(Products products) {
		// TODO Auto-generated method stub
		try {
			sessionFactory.getCurrentSession().delete(products);
			return true;
		} catch (Exception e) {
			// TODO: handle exception
		}
		return false;
	}

	public Products getProductByID(int id) {
		// TODO Auto-generated method stub
		try {
			Products products = (Products) sessionFactory.getCurrentSession()
					.get(Products.class, id);
			return products;
		} catch (Exception e) {
			// TODO: handle exception
		}
		return null;
	}

	@SuppressWarnings("unchecked")
	public List<Products> getAll() {
		// TODO Auto-generated method stub
		try {
			Query query = sessionFactory.getCurrentSession().createQuery(
					"from Products");
			return query.list();
		} catch (Exception e) {
			// TODO: handle exception
		}
		return null;
	}

	public List<Products> getBetween(int min, int max) {
		// TODO Auto-generated method stub
		try {
			List<Products> productsAll = this.getAll();
			return productsAll.subList(min, max);
		} catch (Exception e) {
			// TODO: handle exception
		}
		return null;
	}

}
